<script lang="ts" setup>
import type {
  OnActionClickParams,
  VxeTableGridOptions,
} from '#/adapter/vxe-table';
import type { InspectionRecordModel } from '#/api/equipment/models';

import { Page, useVbenDrawer } from '@vben/common-ui';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import { getInspectionRecordPagedListApi } from '#/api/equipment/inspectionRecord';

import { useColumns, useSearchSchema } from './data';
import Drawer from './modules/drawer.vue';

const [DataDrawer, dataDrawerApi] = useVbenDrawer({
  connectedComponent: Drawer,
  destroyOnClose: true,
});

/**
 * 显示数据
 */
function onOpen(row: InspectionRecordModel) {
  dataDrawerApi.setData(row).open();
}

/**
 * 表格操作按钮的回调函数
 */
function onActionClick({
  code,
  row,
}: OnActionClickParams<InspectionRecordModel>) {
  switch (code) {
    case 'open': {
      onOpen(row);
      break;
    }
  }
}

const [Grid] = useVbenVxeGrid({
  formOptions: {
    collapsed: false,
    fieldMappingTime: [['rangeDate', ['startTime', 'endTime']]],
    schema: useSearchSchema(),
    // 控制表单是否显示折叠按钮
    showCollapseButton: true,
    submitOnEnter: true,
  },
  gridOptions: {
    columns: useColumns(onActionClick),
    border: true,
    stripe: true,
    pagerConfig: {},
    proxyConfig: {
      ajax: {
        query: async ({ page }, formValues) => {
          return await getInspectionRecordPagedListApi({
            pageNumber: page.currentPage,
            pageSize: page.pageSize,
            ...formValues,
          });
        },
      },
    },
  } as VxeTableGridOptions,
});
</script>

<template>
  <Page auto-content-height>
    <DataDrawer class="w-7/12" />
    <Grid />
  </Page>
</template>
